home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11043 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.0 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Getting date in UNIX
  5. Date: 21 Mar 1996 09:27:56 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4is3isINNhdf@keats.ugrad.cs.ubc.ca>
  8. References: <4ik447$bn7@raffles.technet.sg> <4ike32$c8n@ccshst05.cs.uoguelph.ca>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4ike32$c8n@ccshst05.cs.uoguelph.ca>,
  12. Toby K Hay <thay@uoguelph.ca> wrote:
  13. >Leonard Sim (leonard@pacific.net.sg) wrote:
  14. >: Can anyone please inform me how to obtain the date in UNIX C??
  15. >: I just need the date in this format "dd/mm/yyyy". Thanks!!!
  16. >
  17. >I just did this for the first time using an example from my Turbo C 
  18. >reference by Herb Schildt.  (Will mentioning that name start another 
  19. >long thread on his failings?)  
  20. >
  21. >#include "time.h"
  22. >#include "stdio.h"
  23. >int main(void)
  24. >{  struct tm *ptr;
  25. >   time_t lt;
  26. >
  27. >  lt = time(NULL);
  28. >  ptr = localtime(<);
  29. >  printf(asctime(ptr));
  30.  
  31. Is using printf() for a simple string without formatted conversion Schildt's
  32. idea or did you come up with it yourself? :)
  33.  
  34. On some systems, I can still get the C Shell to mess up _today_ because it uses
  35. printf() to print messages that should be done with puts().
  36.  
  37. For instance, if you type !%s%s%s%s%s%s%s%s in the C shell, you will get some
  38. amusing results as it tries to say that it can't find the "%s%s%s..." in your
  39. history. Of course, it echoes that using printf(), which treats the procession
  40. of %s's as conversion specifiers for which no matching arguments exist. 
  41.  
  42. Not that I would expect asctime() to generate a % anywhere, but still...
  43.  
  44. Read the documentation on strftime(). That is the proper way to get the date
  45. into whatever format you want, which is what was asked for. The asctime()
  46. function gives you a the time and date in a predetermined format, not in
  47. "dd/mm/yyyy". It's no good answering the wrong question.
  48.  
  49. By the way, the above can be re-written as ``puts(ctime(<));''.
  50.  
  51. >  return 0;
  52. >}
  53. -- 
  54.  
  55.